<?php
/**
 * PicBoard application renderer
 *
 * File handles input and output to the PicBoard application
 *
 * PHP version 5
 *
 * LICENSE: <LICENCE-HERE>
 *
 * @author     Sebastian Skuse <scs@ecs.soton.ac.uk>
 * @copyright  2009-2010 University of Southampton
 * @license    http://<LICENSE-URL>  <LICENSE-NAME>
 * @version    CVS: $1:$
 */
require_once("system/core/main.php");

// Timer
$start = util::timer();

// Get the language &requested page from the URL.
$language 	= isset($_GET['l']) ? $_GET['l'] : '';
$page		= isset($_GET['page']) ? $_GET['page'] : 'home';
$args		= isset($_GET['args']) ? $_GET['args'] : '';

if(file_exists(SYS_LANGDIR . $language . ".js") == false || $language == "gb") header("Location: en/home");
define('SYS_RUNLANG', $language);

try {
	// Page logic handled by controllers. $page is the name of the controller, prefixed with cntrl.
	$controller = new controller($page);
} catch(PicBoardException $e){
	switch( $e->getCode() ){ 
	 	case 404:
			// Page is not found, load the default controller.
			$controller = new controller('home');
		break;
		
		case 509:
			echo $e->getMessage();
			exit;
		break;
		
		default:
			echo $e->getMessage();
			exit;
		break;
	}
}

// Get the runtime configuration
$runtimeConfiguration = $controller->getRuntimeConfiguration();

// Load the global template into memory
$main = new template("global");

// Add required JS files
$dependencies = $controller->getDependencies();

$dependencyString = "";

if(count($dependencies['JavaScript']) == 0 && count($dependencies['CSS']) == 0 && count($dependencies['InlineJavaScript']) == 0){
	// No external files to include.
	$main->replace("extra-scripts", "");
} else {

	if( isset($dependencies['JavaScript']) ? count($dependencies['JavaScript']) : '' > 0){
		// Javascript files.
		$jsTemplate = new template("fragment.includeJS");
		
		foreach($dependencies['JavaScript'] as $inc){
			$dependencyString .= $jsTemplate->replace("name", $inc)->get();
			// Reset template back to placeholder state so that we can use it again.
			$jsTemplate->reset();
		}
		
	}
	
	if( isset($dependencies['InlineJavaScript']) ? count($dependencies['InlineJavaScript']) : '' > 0){
		// Inline Javascript.
		$jsTemplate = new template("fragment.inlineJS");
		
		foreach($dependencies['InlineJavaScript'] as $inc){
			$dependencyString .= $jsTemplate->replace("data", $inc)->get();
			// Reset template back to placeholder state so that we can use it again.
			$jsTemplate->reset();
		}
		
	}
	
	if( isset($dependencies['CSS']) ? count($dependencies['CSS']) : '' > 0){
		// Javascript files.
		$CSSTemplate = new template("fragment.includeCSS");
		
		foreach($dependencies['CSS'] as $inc){
			
			$CSSTemplate->replace("name", $inc);
			
			//$CSSTemplate->replace("extraAttrs", ($inc == "print.css") ? "media=\"print\"" : "");
			$CSSTemplate->replace("extraAttrs", "");
			
			
			$dependencyString .= $CSSTemplate->get();
			
			// Reset template back to placeholder state so that we can use it again.
			$CSSTemplate->reset();
		}
		
	}

}

// Insert the dependencies into the page.
$main->replace("extra-scripts", $dependencyString);

// Page tools
$main->replace("toolSet", $controller->getTools() );

// Insert username into the page.
$main->replace('name', $controller->getUserNameState() );

// Page Title
$main->replace("pageDescriptorTitle", $controller->getPageName() );

// Make replacements based on page
$main->replace("area", $controller->getArea() );
$main->replace("sideControls", $controller->getSide() );

// Translate the template
$main->performTranslations($language);

// Change URL's to absolutes.
$main->replace("absolute-url", SYS_BASEURL);

//Version number
$version = (SYS_RUNMODE == 0) ? SYS_VER : SYS_VER . "-DEVEL";

// Write out the version number
$main->replace("PBver", $version);

if(SYS_RUNMODE == 1) {
	// Debug
	$db = db::singleton();
	$debugOutput = sprintf("%d queries. execution time %gs", $db->queryCount(), round(util::timer() - $start, 4));
	$main->replace("debug", $debugOutput);
} else {
	$main->replace("debug", "");
}

// Output the template buffer.

switch($runtimeConfiguration['outputMode']){

	case "PDF":
		// PDF output
		$pdf = isset($runtimeConfiguration['PDFOptions']) ? new pdfgen($runtimeConfiguration['PDFOptions']) : new pdfgen();
		$pdf->from_html( $main->get() );
		$pdf->convert();
		$pdf->stream("PicBoard Chart.pdf");
	break;

	case "IMG":
	
	break;

	default:
		// Default HTML
		echo $main->get();
	break;
}

?>